package com.whfc.ms.api.mach.service.impl;

import com.github.pagehelper.PageInfo;
import com.whfc.common.exception.BizException;
import com.whfc.common.result.ListData;
import com.whfc.common.result.PageData;
import com.whfc.common.result.ResultEnum;
import com.whfc.common.util.MathUtil;
import com.whfc.common.util.PageUtil;
import com.whfc.entity.dto.OssPathDTO;
import com.whfc.fuum.dto.SysMachTypeDTO;
import com.whfc.fuum.service.SysDeptService;
import com.whfc.fuum.service.SysMachTypeService;
import com.whfc.mach.dto.*;
import com.whfc.mach.enums.MachState;
import com.whfc.mach.enums.NetState;
import com.whfc.mach.enums.WorkState;
import com.whfc.mach.param.MachPageDataQueryParam;
import com.whfc.mach.param.MachQueryParam;
import com.whfc.mach.service.AppMachDataService;
import com.whfc.ms.api.mach.service.MsMachDataService;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * @author sun_guodong
 * @description
 * @date 2020-11-11
 */
@Service
public class MsMachDataServiceImpl implements MsMachDataService {
    @DubboReference(interfaceClass = SysDeptService.class, version = "1.0.0")
    private SysDeptService sysDeptService;

    @DubboReference(interfaceClass = SysMachTypeService.class, version = "1.0.0")
    private SysMachTypeService sysMachTypeService;

    @DubboReference(interfaceClass = AppMachDataService.class, version = "1.0.0")
    private AppMachDataService appMachDataService;

    @Override
    public ListData<AppMachDTO> list(Integer deptId, Integer machType) {
        return appMachDataService.list(Arrays.asList(deptId), null, null, MachState.IN.getValue(), null, machType);
    }

    @Override
    public PageData<AppMachDTO> list(Integer deptId, Integer pageNum, Integer pageSize, String keyword, Integer machType, Integer state, Integer corpId, Integer bindFlag, Integer netState, Integer workState, Integer machId) {
        List<Integer> deptIds = sysDeptService.getDescendantDeptIdList(deptId);
        if (deptIds == null || deptIds.size() == 0) {
            return PageUtil.pageData(PageInfo.of(Collections.emptyList()));
        }
        MachPageDataQueryParam param = new MachPageDataQueryParam();
        param.setBindFlag(bindFlag);
        param.setCorpId(corpId);
        param.setDeptIds(deptIds);
        param.setKeyword(keyword);
        param.setMachType(machType);
        param.setNetState(netState);
        param.setPageNum(pageNum);
        param.setPageSize(pageSize);
        param.setState(state);
        param.setWorkState(workState);
        param.setMachId(machId);
        PageData<AppMachDTO> pageData = appMachDataService.list(param);
        return pageData;
    }

    @Override
    public MapMachDTO mapList(Integer deptId, Integer corpId, String keyword) {
        List<Integer> deptIds = sysDeptService.getDescendantDeptIdList(deptId);
        List<Integer> corpIds = new ArrayList<>();
        if (corpId != null) {
            corpIds.add(corpId);
        }
        MapMachDTO data = appMachDataService.getMapList(deptId, deptIds, corpIds, null, keyword);
        return data;
    }

    @Override
    public MachDataStatDTO numStat(Integer deptId, String keyword) {
        MachQueryParam param = new MachQueryParam();
        List<Integer> deptIds = sysDeptService.getDescendantDeptIdList(deptId);
        param.setDeptIds(deptIds);
        param.setKeyword(keyword);
        return appMachDataService.machNumstatic(param);
    }

    @Override
    public PageData<AppMachDTO> dataList(Integer deptId, Integer pageNum, Integer pageSize, String keyword, Integer bindFlag, Integer netState, Integer workState) {
        List<Integer> deptIds = sysDeptService.getDescendantDeptIdList(deptId);
        PageData<AppMachDTO> pageData = appMachDataService.dataList(deptIds, pageNum, pageSize, keyword, bindFlag, netState, workState);

        List<AppMachDTO> list = pageData.getList();
        for (AppMachDTO machDTO : list) {
            String icon = sysMachTypeService.getIcon(machDTO.getMachType(), machDTO.getNetState(), machDTO.getWorkState());
            machDTO.setIcon(icon);
        }
        return pageData;
    }

    @Override
    public ListData<AppMachDTO> dataList(Integer deptId, String keyword, Integer deviceType) {
        List<Integer> deptIds = sysDeptService.getDescendantDeptIdList(deptId);
        List<AppMachDTO> list = appMachDataService.dataList(deptIds, keyword, deviceType);
        return new ListData<>(list);
    }

    @Override
    public PageData<AppMachDTO> deviceList(Integer deptId, Integer pageNum, Integer pageSize, String keyword, Date startTime, Date endTime) {
        List<Integer> deptIds = sysDeptService.getDescendantDeptIdList(deptId);
        PageData<AppMachDTO> pageData = appMachDataService.deviceList(deptIds, pageNum, pageSize, keyword, startTime, endTime);
        return pageData;
    }

    @Override
    public AppMachDTO getMachInfo(Integer machId) {
        AppMachDTO machInfo = appMachDataService.getMachInfo(machId);
        if (machInfo == null) {
            throw new BizException(ResultEnum.FAILURE.getCode(), "未找到该设备");
        }
        SysMachTypeDTO subMachType = sysMachTypeService.getParentMachTypeById(machInfo.getMachType());
        if (subMachType != null) {
            machInfo.setSubMachTypeName(subMachType.getName());
            machInfo.setSubMachType(subMachType.getMachTypeId());
        }
        return machInfo;
    }

    @Override
    public OssPathDTO getMachQr(Integer machId) {
        String path = appMachDataService.getMachQr(machId);
        OssPathDTO data = new OssPathDTO();
        data.setPath(path);
        return data;
    }

    @Override
    public AppMachGpsDTO gpsLog(Integer machId, Date date) {
        AppMachGpsDTO data = appMachDataService.gpsLog(machId, date);
        String icon = sysMachTypeService.getIcon(data.getMachType(), NetState.ONLINE.getValue(), WorkState.RUN.getValue());
        data.setIcon(icon);
        return data;
    }

    @Override
    public List<AppMachGpsDTO> getAllMachGpsList(Integer deptId, Date date) {
        List<Integer> deptIds = sysDeptService.getDescendantDeptIdList(deptId);
        return appMachDataService.getAllMachGpsList(deptIds, date);
    }

    @Override
    public MachRunDataDTO getWorkData(Integer machId, Date date) {
        AppMachWorkHourDayDTO machWorkHourDayData = appMachDataService.getMachWorkHourDayData(machId, date);
        MachRunDataDTO machRunDataDTO = new MachRunDataDTO();
        machRunDataDTO.setWorkTimes(MathUtil.round(1.0 * machWorkHourDayData.getWorkTimes() / 3600, 2));
        machRunDataDTO.setStopTimes(MathUtil.round(1.0 * machWorkHourDayData.getStopTimes() / 3600, 2));
        machRunDataDTO.setIdleTimes(MathUtil.round(1.0 * machWorkHourDayData.getIdleTimes() / 3600, 2));
        machRunDataDTO.setOfflineTimes(MathUtil.round(1.0 * machWorkHourDayData.getOfflineTimes() / 3600, 2));
        machRunDataDTO.setTimeList(machWorkHourDayData.getTimesArr());
        return machRunDataDTO;
    }

    @Override
    public MachRunDataDTO getMachWorkAndOilData(Integer machId, Date startDate, Date endDate) {
        return appMachDataService.getMachWorkAndOilData(machId, startDate, endDate);
    }

    @Override
    public MachRadarMapDTO getRadarData(Integer machId, Date startDate, Date endDate) {
        return appMachDataService.getRadarData(machId, startDate, endDate);
    }

    @Override
    public MachRunDataDTO getOilData(Integer machId, Date startDate, Date endDate) {
        return appMachDataService.getOilData(machId, startDate, endDate);
    }

    @Override
    public MachRunDataDTO getOilData(Integer machId, Date date) {
        return appMachDataService.getOilData(machId, date);
    }

    @Override
    public MachLogNumDTO dayReportDate(Integer deptId, Date date) {
        return appMachDataService.dayReportDate(deptId, date);
    }
}
